package icom.acm.manager;

import icom.acm.bean.T_JOB;
import icom.acm.util.DBConnect;

import java.sql.Statement;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

public class T_JobMgr {
	public int insert(final T_JOB job){
		int flag = -1;
		String sql = "insert into T_JOB(J_UID,J_NAME,J_COMPANY) " +
				"values(?, ?, ?)";
		DBConnect db = new DBConnect();
		db.setAutoCommit(false);
		db.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);		
		db.setInt(1, job.getJ_UID());
		db.setString(2, job.getJ_NAME());
		db.setString(3, job.getJ_COMPANY());
		try {
			db.excute();
			flag = db.getGeneratedKeys();
			db.commit();
		} catch (MySQLIntegrityConstraintViolationException e) {
			// 数据重复
			flag = -1;
		} finally{
			db.setAutoCommit(true);
			db.close();
		}
		return flag;
	}
	
	public int update(final T_JOB job){
		int flag = -1;
		String sql = "UPDATE T_JOB SET J_NAME=?, J_COMPANY=? WHERE J_UID=?";
		DBConnect db = new DBConnect();
		db.setAutoCommit(false);
		db.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
		
		db.setString(1, job.getJ_NAME());
		db.setString(2, job.getJ_COMPANY());
		db.setInt(3, job.getJ_UID());
		try {
			db.excute();
			flag = db.getGeneratedKeys();
			db.commit();
		} catch (MySQLIntegrityConstraintViolationException e) {
			// 数据重复
			flag = -1;
		} finally{
			db.setAutoCommit(true);
			db.close();
		}
		return flag;
	}
	public void delete(final int uid, final String company){
		final DBConnect db = new DBConnect();
		final String sql = "DELETE FROM T_JOB WHERE J_UID = ? AND J_COMPANY=?";
		System.out.println(sql);
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		db.setInt(1, uid);
		db.setString(2, company);
		try {
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
}
